<template>
	<view class="userprofile">
		<u-navbar left-icon-color="#fff" :placeholder="true" :bgColor="bgColor" :autoBack="true">
			<view class="navtitle" slot="center">
				群聊设置
			</view>
		</u-navbar>
		<view class="useu-info" v-if="user">
			<view class="user-left">
				<u--image :showLoading="true" style="border-radius: 20rpx;overflow: hidden;" :src="user.group_img"
					loadingIcon="/static/chat/logo.png" errorIcon="/static/chat/logo.png" width="55px"
					height="55px"></u--image>
			</view>
			<view class="user-right">
				<view class="title">{{user.group_name}}</view>
			</view>
		</view>
		<view class="user-name" v-if="user.is_hman == 1" @click="show = true">
			<view>群聊名称</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="user-name" v-if="user.is_hman == 1" @click="upload">
			<view>群聊头像</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="user-name" v-if="user.is_hman == 1" @click="nocshow = true">
			<view>群公告</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="user-name">
			<view>消息免打扰</view>
			<u-switch v-model="switchvalue" @change="change"></u-switch>
		</view>
		<view class="user-name" v-if="user.is_hman == 1" @click="gospeech">
			<view>设置禁言</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="user-name" @click="gouserlist">
			<view v-if="user.is_hman == 1">成员管理</view>
			<view v-else>邀请好友</view>
			<u-icon name="arrow-right" color="#ffffff" size="16"></u-icon>
		</view>
		<view class="fun-btn">
			<u-button type="error" v-if="user.is_hman == 1" class="btn" size="large" text="解散群聊"
				@click="dissolution"></u-button>
			<u-button type="warning" v-else class="btn" size="large" text="退出群聊" @click="exit"></u-button>
		</view>
		<u-popup :show="show" @close="close" mode="center" bgColor="transparent">
			<view class="input-box">
				<view class="title">群聊名称</view>
				<u--input placeholder="请输入群聊名称" customStyle="background-color: #424242; padding: 20rpx; width:450rpx;"
					prefixIconStyle="color: #fff;" prefixIcon="search" :maxlength="11" color="#fff" border="none"
					v-model="value"></u--input>
				<view class="btn">
					<u-button type="primary" color="#5FBC94" @click="gobuy" text="确定"></u-button>
				</view>
			</view>
		</u-popup>
		<u-popup :show="nocshow" @close="nocclose" mode="center" bgColor="transparent">
			<view class="input-box">
				<view class="title">群公告</view>
				<u--textarea v-model="nocvalue" placeholder="请输入群公告" style="width: 500rpx;"  count ></u--textarea>
				<view class="btn">
					<u-button type="primary" color="#5FBC94" @click="gonocbuy" text="确定"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: 'rgba(0,0,0,0.0)',
				id: '',
				user: {},
				userinfo: {},
				show: false,
				value: '',
				switchvalue: false,
				image: '',
				nocshow:false,
				nocvalue:''
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('info')
			this.id = e.id
			this.getuserinfo()
			this.getGroupNotificationStatus()
		},
		methods: {
			nocclose(){
				this.nocshow = false
			},
			upload() {
				let _self = this;
				//第一步:打开手机相册或者文件管理器选择文件
				uni.chooseImage({
					count: 1, //允许上传一张图片
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						//获得选择好的文件,就算只有一个文件也是数组
						const tempFilePaths = res.tempFilePaths;
						uni.showLoading()
						uni.uploadFile({
							url: '/api/upload/upload', //后端接口url
							filePath: tempFilePaths[0],
							header: {
								'Access-Control-Allow-Origin': '*',
							},
							data: {
								token: uni.getStorageSync("token") || ''
							},
							name: 'file', //和后端接口需要的参数名一致
							success: (res) => {
								_self.image = 'https://web.sunsk.top' + JSON.parse(res.data)
									.url
								let params = {
									pic: _self.image,
									group_id:_self.id
								}
								_self.$Request({
									method: 'POST',
									url: '/api/rong/changeimg',
									data: params
								}).then(res => {
									if (res.code == 1) {
										uni.showToast({
											title: "设置成功",
											icon: "none"
										})
										_self.getuserinfo()
									} else {
										uni.showToast({
											title: res.msg,
											icon: "none"
										})
									}
								})
							},
							complete: (ok) => {
								uni.hideLoading()
							}
						})
					}
				})
			},
			gouserlist() {
				uni.navigateTo({
					url: '/pages/chat/groupuserlist/index?id=' + this.id + '&ishman=' + this.user.is_hman
				})
			},
			gospeech() {
				uni.navigateTo({
					url: '/pages/chat/groupspeech/index?id=' + this.id
				})
			},
			exit() {
				let app = this
				uni.showModal({
					title: '提示',
					content: '是否退出此群！',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.showLoading({
								mask: true
							})
							app.$Request({
								method: 'POST',
								url: '/api/rong/exitGroup',
								data: {
									group_id: app.id,
								}
							}).then(res => {
								if (res.code == 1) {
									const conversationType = app.$RongIMLib.ConversationType.GROUP;
									const targetId = app.id;
									app.$RongIMLib.removeConversation({
										conversationType,
										targetId: targetId,
									}).then(res => {
										// 删除指定会话成功
										if (res.code === 0) {
											console.log(res.code)
											uni.showToast({
												title: '退出成功',
												icon: 'none'
											})
											setTimeout(() => {
												uni.navigateBack({
													delta: 2
												})
											}, 1000)
										} else {
											console.log(res.code, res.msg)
										}
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			dissolution() {
				let app = this
				uni.showModal({
					title: '提示',
					content: '是否解散此群！',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.showLoading({
								mask: true
							})
							app.$Request({
								method: 'POST',
								url: '/api/rong/dismissGroup',
								data: {
									group_id: app.id,
								}
							}).then(res => {
								uni.hideLoading()
								if (res.code == 1) {
									const conversationType = app.$RongIMLib.ConversationType.GROUP;
									const targetId = app.id;
									app.$RongIMLib.removeConversation({
										conversationType,
										targetId: targetId,
									}).then(res => {
										// 删除指定会话成功
										if (res.code === 0) {
											console.log(res.code)
											uni.showToast({
												title: '解散成功！',
												icon: 'none'
											})
											setTimeout(() => {
												uni.navigateBack({
													delta: 2
												})
											}, 1000)
										} else {
											console.log(res.code, res.msg)
										}
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getGroupNotificationStatus() {
				const conversationType = this.$RongIMLib.ConversationType.GROUP;
				const targetId = this.id;
				this.$RongIMLib.getConversationNotificationLevel({
					conversationType,
					targetId
				}).then(
					res => {
						if (res.code == 0) {
							console.log(res)
							if (res.data == 5) {
								this.switchvalue = true
							} else {
								this.switchvalue = false
							}
						}
					}).catch(err => {
					console.log(err)
					setTimeout(() => {
						this.getGroupNotificationStatus()
					}, 1000)
				})
			},
			change(e) {
				console.log(e)
				if (e == true) {
					const conversationType = this.$RongIMLib.ConversationType.GROUP
					const targetId = this.id
					const notificationLevel = this.$RongIMLib.NotificationLevel.NOT_MESSAGE_NOTIFICATION
					this.$RongIMLib.setConversationNotificationLevel({
						conversationType,
						targetId
					}, notificationLevel).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '设置成功！',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '设置失败！',
								icon: 'none'
							})
						}
					})
				} else {
					const conversationType = this.$RongIMLib.ConversationType.GROUP
					const targetId = this.id
					const notificationLevel = this.$RongIMLib.NotificationLevel.ALL_MESSAGE
					this.$RongIMLib.setConversationNotificationLevel({
						conversationType,
						targetId
					}, notificationLevel).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: '设置成功！',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '设置失败！',
								icon: 'none'
							})
						}
					})
				}
			},
			gonocbuy(){
				if (this.nocvalue == '') {
					uni.showToast({
						title: '请输入群公告！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				this.$Request({
					method: 'POST',
					url: '/api/rong/editGroupInfo',
					data: {
						group_id: this.id,
						type: 2,
						content: this.nocvalue
					}
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.nocshow = false
						uni.showToast({
							title: '设置成功！',
							icon: 'none'
						})
						this.getuserinfo()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			gobuy() {
				if (this.value == '') {
					uni.showToast({
						title: '请输入群聊名称！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				this.$Request({
					method: 'POST',
					url: '/api/rong/editGroupInfo',
					data: {
						group_id: this.id,
						type: 1,
						content: this.value
					}
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.show = false
						uni.showToast({
							title: '设置成功！',
							icon: 'none'
						})
						this.getuserinfo()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})

			},
			close() {
				this.show = false
			},
			getuserinfo() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupInfo',
					data: {
						group_id: this.id,
					}
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.nocvalue = res.group_info.group_notice
						this.user = res.group_info
						let list = res.data
						list.map(item => {
							if (item.id == this.userinfo.member_id) {
								this.user.is_hman = item.is_hman
							}
						})
					}
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #17191A;
	}
</style>
<style scoped lang="scss">
	.userprofile {
		.fun-btn {
			width: 92%;
			margin: 0 auto;

			.btn {
				margin-top: 30rpx;
			}
		}

		.navtitle {
			color: #fff;
		}

		.input-box {
			background-color: #232526;
			padding: 30rpx;
			color: #fff;
			border-radius: 20rpx;

			.title {
				padding-bottom: 30rpx;
				text-align: center;
			}

			.btn {
				margin-top: 30rpx;
			}
		}

		.useu-info {
			display: flex;
			align-items: center;
			padding: 40rpx 30rpx;

			.user-left {
				margin-right: 20rpx;

				image {
					width: 110rpx;
					height: 110rpx;
				}
			}

			.user-right {
				.title {
					font-size: 32rpx;
					color: #fff;
					font-weight: 600;
				}
			}
		}

		.user-name {
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20rpx 30rpx;
			color: #fff;
			background-color: #232526;
			padding: 25rpx 20rpx;
			border-radius: 10rpx;
		}
	}
</style>